package com.sunmi.usercenter.controller;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sunmi.usercenter.HttpUrlConstant;
import com.sunmi.usercenter.bean.GetShopType;
import com.sunmi.usercenter.bean.rpc.MD5Req;
import com.sunmi.usercenter.bean.rpc.ReqCheckUserName;
import com.sunmi.usercenter.bean.rpc.ReqGetUserInfo;
import com.sunmi.usercenter.bean.rpc.ReqGetVerificationCode;
import com.sunmi.usercenter.bean.rpc.ReqLogin;
import com.sunmi.usercenter.bean.rpc.ReqReSetPassword;
import com.sunmi.usercenter.bean.rpc.ReqSetPassword;
import com.sunmi.usercenter.bean.rpc.ReqUpdatePassword;
import com.sunmi.usercenter.bean.rpc.ReqUpdateShopAddress;
import com.sunmi.usercenter.bean.rpc.ReqUpdateShopName;
import com.sunmi.usercenter.bean.rpc.ReqUpdateShopType;
import com.sunmi.usercenter.bean.rpc.ReqUpdateShopUserName;
import com.sunmi.usercenter.bean.rpc.ReqVerifyCode;
import com.sunmi.usercenter.bean.rpc.ReqVerifyPassword;
import com.sunmi.usercenter.service.HttpClient;

import android.content.Context;
import android.widget.Toast;

/**
 * 项目名称：Sunmi-User-Center
 * 类描述：网络请求
 * 创建人：Abtswiath丶lxy
 * 创建时间：2016/6/8 16:31
 * 修改人：longx
 * 修改时间：2016/6/8 16:31
 * 修改备注：
 */
public class HttpController {

    @SuppressWarnings("unused")
    private Context mContext;

    private static HttpController instance;

    public HttpController(Context context) {
        this.mContext = context;
    }

    public static HttpController getInstance(Context context) {
        if (instance == null) {
            synchronized (HttpController.class) {
                if (instance == null) {
                    instance = new HttpController(context);
                }
            }
        }
        return instance;
    }

    /**
     * 判断此手机号码或者邮箱是否注册过
     */
    public void checkUsername(ReqCheckUserName checkUserName, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
        MD5Req<ReqCheckUserName> req = new MD5Req<ReqCheckUserName>(checkUserName);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.CHECK_USERNAME, params, asyncHttpResponseHandler);
    }

    /**
     * 注册获取验证码
     */
    public void getVerificationCode(ReqGetVerificationCode getVerificationCode, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
        MD5Req<ReqGetVerificationCode> req = new MD5Req<ReqGetVerificationCode>(getVerificationCode);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.REGISTERED_SEND_MESSAGE, params, asyncHttpResponseHandler);
    }

    /**
     * 找回密码获取验证码
     */
    public void getVerificationCode4Retrieve(ReqGetVerificationCode getVerificationCode, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
        MD5Req<ReqGetVerificationCode> req = new MD5Req<ReqGetVerificationCode>(getVerificationCode);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.RETRIEVE_SEND_MESSAGE, params, asyncHttpResponseHandler);
    }

    /**
     * 设置密码
     */
    public void setPassword(ReqSetPassword reqSetPassword, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
        MD5Req<ReqSetPassword> req = new MD5Req<ReqSetPassword>(reqSetPassword);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.REGISTER, params, asyncHttpResponseHandler);
    }

    /**
     * 登录
     */
    public void login(ReqLogin reqLogin, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
        MD5Req<ReqLogin> req = new MD5Req<ReqLogin>(reqLogin);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.LOGIN, params, asyncHttpResponseHandler);
    }

    /**
     * 验证密码是否正确
     */
    public void verifyPassword(ReqVerifyPassword reqVerifyPassword, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
        MD5Req<ReqVerifyPassword> req = new MD5Req<ReqVerifyPassword>(reqVerifyPassword);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.VERIFY_PASSWORD, params, asyncHttpResponseHandler);
    }

    /**
     * 更新店铺名
     */
    public void updateShopname(ReqUpdateShopName reqUpdateShopName, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
        MD5Req<ReqUpdateShopName> req = new MD5Req<ReqUpdateShopName>(reqUpdateShopName);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.UPDATE_SHOP_NAME, params, asyncHttpResponseHandler);
    }

    /**
     * 修改店铺地址
     */
    public void updateShopAddress(ReqUpdateShopAddress shopAddress, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
        MD5Req<ReqUpdateShopAddress> req = new MD5Req<ReqUpdateShopAddress>(shopAddress);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.UPDATE_SHOP_ADDRESS, params, asyncHttpResponseHandler);
    }

    /**
     * 修改店铺类型
     */
    public void updateShopType(ReqUpdateShopType reqUpdateShopType, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
//        Toast.makeText(mContext,reqUpdateShopType.getuId(),Toast.LENGTH_LONG).show();
        MD5Req<ReqUpdateShopType> req = new MD5Req<ReqUpdateShopType>(reqUpdateShopType);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.UPDATE_SHOP_TYPE, params, asyncHttpResponseHandler);
    }

    /**
     * 修改店铺联系人姓名
     */
    public void updateShopUserName(ReqUpdateShopUserName reqUpdateShopUserName, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
//        Toast.makeText(mContext,reqUpdateShopUserName.getuId(),Toast.LENGTH_LONG).show();
        MD5Req<ReqUpdateShopUserName> req = new MD5Req<ReqUpdateShopUserName>(reqUpdateShopUserName);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.UPDATE_CONTACT_NAME, params, asyncHttpResponseHandler);
    }

    /**
     * 得到所有店铺类型
     */
    public void getAllShopType(AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
        MD5Req<GetShopType> req = new MD5Req<GetShopType>(null);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.GET_ALL_SHOP_TYPE,params, asyncHttpResponseHandler);
    }

    /**
     * 根据用户id得到所有信息
     */
    public void getUserInfo(ReqGetUserInfo reqGetUserInfo, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
        MD5Req<ReqGetUserInfo> req = new MD5Req<ReqGetUserInfo>(reqGetUserInfo);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.GET_INFO_4_UID, params, asyncHttpResponseHandler);
    }

    /**
     * 修改密码
     */
    public void updatePassword(ReqUpdatePassword reqUpdatePassword, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
        MD5Req<ReqUpdatePassword> req = new MD5Req<ReqUpdatePassword>(reqUpdatePassword);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.UPDATE_PASSWORD, params, asyncHttpResponseHandler);
    }

    /**
     * 验证验证码是否正确
     */
    public void verifyCode(ReqVerifyCode reqVerifyCode, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
        MD5Req<ReqVerifyCode> req = new MD5Req<ReqVerifyCode>(reqVerifyCode);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.VERIFY_CODE, params, asyncHttpResponseHandler);
    }

    /**
     * 重置密码
     */
    public void reSetPassword(ReqReSetPassword reqReSetPassword, AsyncHttpResponseHandler asyncHttpResponseHandler) {
        RequestParams params = new RequestParams();
        MD5Req<ReqReSetPassword> req = new MD5Req<ReqReSetPassword>(reqReSetPassword);
        params.put("params", req.jsonParams);
        params.put("sign", req.sign);
        params.put("isEncrypted", req.isEncrypted);
        params.put("randomNum", req.randomNum);
        params.put("timeStamp", req.timeStamp);
        HttpClient.post(HttpUrlConstant.RESET_PASSWORD, params, asyncHttpResponseHandler);
    }

}
